package com.example.a22310.myapplication.Fragment;


import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.example.a22310.myapplication.Fragment.Train.AllFragment;
import com.example.a22310.myapplication.Fragment.Train.BirdFragment;
import com.example.a22310.myapplication.Fragment.Train.DoorFragment;
import com.example.a22310.myapplication.Fragment.Train.SeliorFragment;
import com.example.a22310.myapplication.R;
import com.example.a22310.myapplication.Third.Banners.BannerView;

import java.util.ArrayList;
import java.util.List;


public class TrainFragment extends Fragment {
    private View view;
    private RadioGroup radioGroup;
    private android.app.FragmentManager manager;
    private FragmentTransaction transaction;
    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;

    private List<View> viewList;
    private BannerView bannerView;
    private int[] imgs = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_train, container, false);
        bannerView = (BannerView)view.findViewById(R.id.banner);
        initialize();
        return view;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        radioGroup = (RadioGroup) view.findViewById(R.id.main_tab_RadioGroup);
        manager = getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio_bird:
                        mTab01 = new BirdFragment();
                        replace(mTab01);
                        break;
                    case R.id.radio_door:
                        mTab02 = new DoorFragment();
                        replace(mTab02);
                        break;
                    case R.id.radio_selior:
                        mTab03 = new SeliorFragment();
                        replace(mTab03);
                        break;
                    case R.id.radio_all:
                        mTab04 = new AllFragment();
                        replace(mTab04);
                        break;
                }

            }
        });

        radioGroup.check(R.id.radio_all);


    }

    private void  replace(Fragment fragment){

        transaction=manager.beginTransaction();
        transaction.replace(R.id.frame_train,fragment);
        transaction.commit();


    }

    public TrainFragment() {
    }


    private void initialize(){

        viewList = new ArrayList<View>();

        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }
        bannerView.startLoop(true);

        bannerView.setViewList(viewList);
        bannerView.setTransformAnim(true);
    }
}
